<?php

namespace app\admin\controller\coupon;

use app\common\controller\Backend;

// 优惠券
class Coupon extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Coupon.Coupon');
        $this->view->assign("statusList", $this->model->statuslist());
    }

    // 查看
    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            // var_dump($where['status']);exit;

            $total = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    // ->fetchSql(true)
                    ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    
    //添加
    public function add()
    {
        if ($this->request->isPost())
        {
            // 获取表单数据
            $params = $this->request->post('row/a');
            $params['endtime'] = strtotime($params['endtime']);

            // 添加校验器
            $result = $this->model->save($params);

            if ($result === false)
            {
                $this->error($this->model->getError());
                exit;
            }

            $this->success();
        }
        return $this->view->fetch();
    }

    // 编辑
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);

        if (!$row)
        {
            $this->error(__('优惠券不存在'));
        }

        if ($this->request->isPost())
        {
            // 获取表单数据
            $params = $this->request->post('row/a');
            $params['id'] = $ids;
            $params['endtime'] = strtotime($params['endtime']);

            $result = $this->model->isUpdate(true)->save($params);

            if ($result === false)
            {
                $this->error($this->model->getError());
                exit;
            }

            //判断是否有上传新图片
            if ($row['thumb'] != $params['thumb'])
            {
                //不相等就说明有换图片了，就删除掉旧图片
                @is_file("." . $row['thumb']) && @unlink("." . $row['thumb']);
            }


            $this->success();
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    // 删除
    public function del($ids = null)
    {
        $row = $this->model->select($ids);

        if (!$row)
        {
            $this->error(__('优惠券不存在'));
        }

        $result = $this->model->destroy($ids);

        if ($result === false)
        {
            $this->error($this->model->getError());
            exit;
        }

        // 删除课程图片
        foreach($row as $item)
        {
            @is_file("." . $item['thumb']) && @unlink("." . $item['thumb']);
        }

        $this->success();
    }
}